﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure;

namespace FtpWebRole.Entities
{
    public class UserContext : TableServiceContext
    {
        private CloudStorageAccount _account;
        private readonly string _tableName = "User";

        public UserContext(CloudStorageAccount account)
            : base(account.TableEndpoint.AbsoluteUri, account.Credentials)
        {
            _account = account;

            var client = new CloudTableClient(_account.TableEndpoint.AbsoluteUri, _account.Credentials);
            client.CreateTableIfNotExist(_tableName);
        }

        public IQueryable<UserEntity> UserEntities
        {
            get
            {
                return CreateQuery<UserEntity>(_tableName);
            }
        }

        public void AddUser(UserEntity user)
        {
            AddObject(_tableName, user);
            SaveChanges();
        }

        public void DeleteUser(UserEntity user)
        {
            DeleteObject(user);
            SaveChanges();
        }

        public void UpdateUser(UserEntity user)
        {
            UpdateObject(user);
            SaveChanges();
        }
    }
}
